I need to implement something like "that particular value was emitted by publisher at least once in the past". So basically true/false. As an example to answer if zero was ever send here:
let publisher = PassthroughSubject<Int, Never>()
...
for _ in 1...3 {
publisher.send(Int.random(in: 0...2))
}
I came up only with the following options and both feels unnatural somehow:
//Option #1 Using scan
let hadZero = publisher
.scan(false) { $0 || $1 == 0 }
//Option #2 Merging with CurrentValueSubject
let hadZero = Publishers.Merge(
CurrentValueSubject<Bool, Never>(false),
publisher.contains { $0 == 0 }
)
Is there any better way to do it?
You can use the contains operator, which emits true immediately when the value sought is emitted by its upstream or emits false when the upstream completes without having emitted the sought value.
let hadZero = publisher.contains(0)